//
// incfix.cpp
// incfix
// Fixes include files for deployment as part of the Protea Project SDK
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

const long MAX_LINE_SIZE = 1024;

int main( int argc, char* argv[] ) {
	
	if (argc != 3) {
		cout << "Include fixer" << endl;
		cout << argc << endl;
		cout << "incfix source target" << endl;
		return -1;
	};
	cout << "Fixing include file: " << argv[1] << endl;

	fstream input, output;
	char buffer[ MAX_LINE_SIZE + 1 ];
	char* find;

	input.open( argv[1], ios::in );
	output.open( argv[2], ios::out );

	while (!input.eof()) {
		input.getline( buffer, MAX_LINE_SIZE );

		// Fix include paths to exist in a single (flat) SDK include directory
		if (find = strstr( buffer, "\"../" )) {
			char* counter = find + strlen( find );
			*find = 0;
			while (*counter != '/')
				counter--;
			output << buffer << "\"" << counter+1 << endl;

		// Remove __EXPORT macros
		} else if (find = strstr( buffer, "__EXPORT" )) {
			if (!strstr( argv[1], "defines.h" ))
				memcpy( find, "        ", 8 );
			output << buffer << endl;

		// Remove OSDep directory names to ensure a single (flat) SDK include directory
		} else if (find = strstr( buffer, "OSDep" )) {
			char* counter = find + 6;
			*find = 0;
			output << buffer << counter << endl;
		} else 
			output << buffer << endl;
	};

	input.close();
	output.close();
	return 0;
};
